﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Solidinfo.Model;
using Solidinfo.DAL;
using System.Linq.Expressions;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace Solidinfo.Web.Helpers
{
    public static class MultilingualHelper
    {
        /// <summary>
        /// 输出多语言支持的文本
        /// </summary>
        /// <param name="html">htmlHelper</param>
        /// <param name="stringName">文本名称</param>
        /// <returns>多语言输出</returns>
        public static string Multilingual(this HtmlHelper html, string stringName)
        {
            return GetMultilingualString(stringName);
        }
        /// <summary>
        /// 获取多语言支持的文本
        /// </summary>
        /// <param name="stringName">文本名称</param>
        /// <returns>多语言输出</returns>
        public static string GetMultilingualString(this Controller controller, string stringName)
        {
            return GetMultilingualString(stringName);
        }

        public static string GetMultilingualString(string stringName)
        {
            return MultilingualStringGetter.Instance.GetMultilingualString(stringName);
        }

        public static string GetMultilingualDisplay(string entityName, string columnName)
        {
            return MultilingualEntityColumnDisplayNameGetter.Instance.GetMultilingualEntityColumnDisplayName(entityName, columnName);
        }


        public static string MultilingualDisplay<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression)
        {
            return html.DisplayNameFor(expression).ToString();
        }

        public static string MultilingualDisplay<TModel>(this HtmlHelper<TModel> html, string entityName, string columnName)
        {
            return GetMultilingualDisplay(entityName, columnName);
        }

        public static string MultilingualPlateCode(this HtmlHelper html, string stringName)
        {
            return GetMultilingualPlateCodeStringFor(stringName);
        }

        public static string MultilingualPlateCodeFor<TModel, TProperty>(this HtmlHelper html, Expression<Func<TModel, TProperty>> expression)
        {
            return GetMultilingualPlateCodeStringFor<TModel, TProperty>(expression);
        }

        public static string MultilingualPlateCodeFor(this HtmlHelper html, string stringName)
        {
            return GetMultilingualPlateCodeStringFor(stringName);
        }

        public static string MultilingualPlateCodeFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression)
        {
            return GetMultilingualPlateCodeStringFor<TModel, TProperty>(expression);
        }

        public static string MultilingualPlateCodeFor<TModel, TProperty>(this HtmlHelper<IPager<TModel>> html, Expression<Func<TModel, TProperty>> expression)
            where TModel : class
        {
            return GetMultilingualPlateCodeStringFor<TModel, TProperty>(expression);
        }

        public static string MultilingualPlateCodeFor<TModel, TFilter, TProperty>(this HtmlHelper<IPager<TModel, TFilter>> html, Expression<Func<TModel, TProperty>> expression)
            where TModel : class
            where TFilter : new()
        {
            return GetMultilingualPlateCodeStringFor<TModel, TProperty>(expression);
        }

        public static string MultilingualPlateCodeForFilter<TModel, TFilter, TProperty>(this HtmlHelper<IPager<TModel, TFilter>> html, Expression<Func<TFilter, TProperty>> expression)
            where TModel : class
        {
            return GetMultilingualPlateCodeStringFor<TFilter, TProperty>(expression);
        }

        public static string GetMultilingualPlateCodeStringFor<TModel,TValue>(Expression<Func<TModel,TValue>> expression)
        {
            return MultilingualTemplate("PlateCode", DisplayNameExtensions.GetDisplayNameString<TModel, TValue>(expression));
        }

        public static string GetMultilingualPlateCodeStringFor(string stringName)
        {
            return MultilingualTemplate("PlateCode", GetMultilingualString(stringName));
        }

        public static MvcHtmlString GetMultilingualPlateCodeMvcStringFor<TModel, TValue>(Expression<Func<TModel, TValue>> expression)
        {
            return new MvcHtmlString(HttpUtility.HtmlEncode(MultilingualTemplate("PlateCode", DisplayNameExtensions.GetDisplayNameString<TModel, TValue>(expression))));
        }


        public static string MultilingualTemplate(string templateName, params string[] contents)
        {
            var location = HttpMultilingualLocationGetter.Instance.GetCurrentLocation();
            string template = GetMultilingualTemplate(templateName, location);


            return string.Format(template, contents);
        }

        private static readonly IMultilingualTemplateGetter _templateGetter = DefaultMultilingualTemplateGetter.Instance;

        public static string GetMultilingualTemplate(string templateName, string location)
        {
            return _templateGetter.GetTemplate(templateName, location);

        }

    }

    public interface IMultilingualTemplateGetter
    {
        string GetTemplate(string templateName, string location);
    }

    public class DefaultMultilingualTemplateGetter : IMultilingualTemplateGetter
    {
        private static readonly DefaultMultilingualTemplateGetter __instance = new DefaultMultilingualTemplateGetter();
        public static DefaultMultilingualTemplateGetter Instance
        {
            get
            {
                return __instance;
            }
        }
        protected DefaultMultilingualTemplateGetter()
        {

        }
        public string GetTemplate(string templateName, string location)
        {
            switch (templateName)
            {
                case "PlateCode":
                    string enterStr;
                    switch (location)
                    {
                        case "zh-cn":
                            enterStr = "请输入";
                            break;
                        case "en-us":
                            enterStr = "Enter";
                            break;
                        default:
                            enterStr = "请输入";
                            break;
                    }
                    return string.Format("{0} {{0}}", enterStr);
                default:
                    break;
            }
            return string.Empty;
        }
    }



}